package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public Object register(User user){
        JSONResult jsonResult = new JSONResult();
        try {
            userService.queryUserInfo(user);
            userService.saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

    @PutMapping("{id}")
    public Object update(User user){
        userService.saveOrUpdate(user);
        return new JSONResult().setResult(user);
    }

    @GetMapping("{authorId}/travels")
    public PageInfo query(TravelQueryObject qo){
        qo.setOrderby("t.lastUpdateTime desc");
        return travelService.queryForList(qo);
    }

    @GetMapping("{id}")
    public User getById(User user){
        return userService.getById(user);
    }
}
